package com.farm.utils_pay;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;

import com.farm.utils_all.Constants;
import com.farm.utils_all.Md5Utils;



public class Sign {

	public static String sign() {
		String appid = Constants.APPID;
		String mch_id = Constants.MCHID;
		String device_info = "1000";
		String body = "test";
		String nonce_str = "ibuaiVcKdpRxkhJA";

		SortedMap<Object, Object> parameters = new TreeMap<Object, Object>();
		parameters.put("appid", appid);
		parameters.put("mch_id", mch_id);
		parameters.put("device_info", device_info);
		parameters.put("body", body);
		parameters.put("nonce_str", nonce_str);

		String sign = creatSign(parameters);
		return sign;

	}

	public static String creatSign(SortedMap<Object, Object> parameters) {
		StringBuffer sb = new StringBuffer();
		Set es = parameters.entrySet();
		Iterator<Object> it = es.iterator();
		while (it.hasNext()) {
			Map.Entry entry = (Map.Entry) it.next();
			String k = entry.getKey().toString();
			Object v = entry.getValue();
			if (null != v && !"".equals(v) && !"sign".equals(k) && !"key".equals(k)) {
				sb.append(k + "=" + v + "&");
			}
		}
		sb.append("key=" + Constants.WXAPPKEY);
		String sign = Md5Utils.MD5Encode(sb.toString(), "UTF-8").toUpperCase();
		// System.out.println(sign);
		return sign;

	}

	public static void main(String[] args) {
		sign();
	}
}
